<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 聂尧 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
//公共函数


//<********************************************模板引擎中转换成文字的函数******************************************>
//角色ID 换成角色名称
function role_id_name($role_id)
{
    switch($role_id)
    {
        case 0:
            return "超级管理员";
            break;
        case 1:
            return "商家";
            break;
        case 2:
            return "客服";
            break;
        default:
            return "其他角色";
            break;
    }
}

//开始编码转换成文字
function is_click($is_click)
{
    switch($is_click)
    {
        case 0:
            return "<span class=\"label label-success radius\">开启</span>";
            break;
        case 1:
            return "<span class=\"label label-error radius\">关闭</span>";
            break;
        default:
            return "<span class=\"label label-999 radius\">等待</span>";
            break;

    }
}

//icon 转换字符串
function icon()
{

}

//图文模块中 图文消息类型 转文字
function textpic_type($type)
{
    switch ($type)
    {
        case 0: return "适应于所有";
        break;
        case 1: return "关注回复";
        break;
        case 2: return "点击事件回复";
        break;
        default: return "暂时未定义";
        break;
    }
}

//微信自定义菜单中类型的转换1:代表链接 2:代表按钮
function wxmenu_type($type)
{
    switch ($type)
    {
        case 1:
            return "链接";
            break;
        case 2:
            return "按钮";
            break;
        default:return"未知";
            break;
    }
}
//<***************************************************end 模板字符串转换函数*************************************>

//生成二维码函数
// $url=>对应链接地址  $id 对应后面的参数
function getqrcode($url,$id)
{
    vendor("phpqrcode.phpqrcode");
    // 纠错级别：L、M、Q、H
    $level = 'L';
    // 点的大小：1到10,用于手机端4就可以了
    $size = 4;
    // 生成的文件名  根据需求更改
    $fileName = 'uploads/qrcode/zhibo'.$id.'.png';
    QRcode::png($url, $fileName, $level, $size);
}




/*****************************************************短信验证码函数_begin****************************************/
//发送短息接口 美圣公司
function send_smscode($mobile,$code)
{
    $svr_url = 'http://112.74.76.186:8030/service/httpService/httpInterface.do';   // 服务器接口路径
    $svr_param = array();
    $svr_param['username'] = 'JSM41832';    // 账号
    $svr_param['password'] = 'r66tzlf0';    // 密码
    $svr_param['veryCode'] = 'y41hvtn3d325';    // 通讯认证Key
    $post_data = $svr_param;
    $post_data['method'] = 'sendMsg';
    $post_data['mobile'] = $mobile;
    $post_data['content']= '@1@='.$code;//多个参数以英文逗号隔开
    $post_data['msgtype']= '2';             // 1-普通短信，2-模板短信
    $post_data['tempid'] = 'JSM41832-0002'; // 模板编号
    $post_data['code']   = 'utf-8';         // utf-8,gbk
    $res =$this->request_post($svr_url, $post_data);  // 如果账号开了免审，或者是做模板短信，将会按照规则正常发出，而不会进人工审核平台
    //$this->echo_xmlarr($res);

}
/**
 * 模拟post进行url请求
 * @param string $url
 * @param array $post_data
 */
function request_post($url = '', $post_data = array()) {
    if (empty($url) || empty($post_data)) {
        return false;
    }
    $o = "";
    foreach ( $post_data as $k => $v )
    {
        $o.= "$k=" . urlencode( $v ). "&" ;
    }
    $post_data = substr($o,0,-1);

    $postUrl = $url;
    $curlPost = $post_data;
    $ch = curl_init();//初始化curl
    curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
    curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    $data = curl_exec($ch);//运行curl
    curl_close($ch);

    return $data;
}


// XML格式转数组格式
function xml_to_array( $xml ) {
    $reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
    if(preg_match_all($reg, $xml, $matches)) {
        $count = count($matches[0]);
        for($i = 0; $i < $count; $i++) {
            $subxml= $matches[2][$i];
            $key = $matches[1][$i];
            if(preg_match( $reg, $subxml )) {
                $arr[$key] = xml_to_array( $subxml );
            } else {
                $arr[$key] = $subxml;
            }
        }
    }
    return $arr;
}

// 页面显示数组格式，用于调试
function echo_xmlarr($res) {
    $res = xml_to_array($res);
    echo "<pre>";
    print_r($res);
    echo "</pre>";
}


//判断手机号 是啦个运营商
function indent_mobile($mobile)
{
    //移动开头
    $Move=array('134','135','136','137','138','139','147','150','151','152','157','158','159','182','184','187','188');
    //联通
    $Unicom=array('130','131','132','145','155','156','185','186');
    //电信
    $Telecom=array('133','1349','153','180','181','189');
    $string =substr($mobile,0,3);
    foreach ($Move as $key=>$val){if($string==$val){return $mobile." 移动";}}
    foreach ($Unicom as $key=>$val){if($string==$val){return $mobile." 联通";}}
    foreach ($Telecom as $key=>$val){if($string==$val){return $mobile." 电信";}}
    return $mobile;
}


//根据给的时间戳和当前时间戳作对比
function begintime($time)
{
    $chatime=$time-time();
    if($chatime>0)
    {
        if(abs($chatime)<3600)
        {
        return number_format(abs($chatime)/60,0)."分钟后";
        }   
        else if(abs($chatime)>3600&&abs($chatime)<86400)
        {
        return number_format(abs($chatime)/3600,0)."小时后";  
        }
        else{
               return number_format(abs($chatime)/86400,0)."天后";  
        }
    }
    else
    {
        return "待直播";
    }

     
}

//是不是精选
function is_good($key)
{
    if($key==1)
    {
        return "<a style='text-decoration:none;color:green;'>是</a>";
    }
    else
    {
        return "否";
    }
}

//对游览次数加倍处理
function beishu($number)
{
    $bili=file_get_contents('beishu.txt');
    return (int)$number*(int)$bili;
}

//根据t算出语音的长度
function voice_length($t)
{
   return $t/1.2+20;
}

//根据钱算出
function room_price($money)
{
	$money=(float)$money;
    if($money==0)
    {
        return "￥免费";
    }
    else
    {
        return "￥".number_format($money,2);
    }
}
//根据数字算出来是不是要加万
function add_wan($number)
{
    $bili=file_get_contents('beishu.txt');
    $number=(int)$number*(int)$bili;
    if($number>=10000)
    {
        return number_format($number/10000,1)."万+";
    }
    else
    {
        return $number;
    }
}

//根据数字改变审核状态
function shenghei_zhuangtai($number)
{
  if($number==0)
  {
   return "审核中";
  }
  else if($number==-1)
  {
   return "已拒绝"; 
  } 
  else{
   return "已通过";
  }    
}
//根据钱的金额保留小数后两位
function money_baoliu($money)
{
    $money=(float)$money;
    return number_format($money,2);
}
//累计收益的钱保留
function money_profit($money)
{
    return number_format($money/100,2);
}
//access_token 缓存文件7000秒有效期
//判断是不是有限
function jugement_token_value()
{
    $string=file_get_contents('token.txt');
    if(!$string)
        return false;
    $data=json_decode($string);
    if(time()-intval($data->time_out)<7200)
        return true;
    return false;
}
//得到access_token
function get_access_token_ache()
{
    $string=file_get_contents('token.txt');
    $data=json_decode($string);
    return $data->access_token;
}